//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "PagodaOption.h"
using namespace Cephei::QL::Experimental;
#include <gen/QL/Payoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/MultiAssetOption.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::CPagodaOption::CPagodaOption (Cephei::IVector<DateTime>^ fixingDates, Double roof, Double fraction, Cephei::QL::IPricingEngine^ QL_Pricer) : CMultiAssetOption(CPagodaOption::typeid)
{
    CoVector<DateTime>^ _CfixingDates;
    try
    {
#ifdef HANDLE
        _phPagodaOption = NULL;
#endif
        CoVector<DateTime>^ _CfixingDates = safe_cast<CoVector<DateTime>^> (fixingDates);
        _CfixingDates->Lock();
        INativeVector<DateTime>^ _NCIfixingDates = _CfixingDates->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCfixingDates = safe_cast<CDateTimeVector^>(_NCIfixingDates);
        std::vector<QuantLib::Date>& _fixingDates = static_cast<std::vector<QuantLib::Date>&> (_NCfixingDates->GetReference ());
        QuantLib::Real _roof = (QuantLib::Real)ValueHelper::Convert (roof);
        QuantLib::Real _fraction = (QuantLib::Real)ValueHelper::Convert (fraction);
        _ppPagodaOption = new boost::shared_ptr<QuantLib::PagodaOption> (new QuantLib::PagodaOption ( _fixingDates,  _roof,  _fraction ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppPagodaOption)->setPricingEngine (_QL_Pricer);
        SetMultiAssetOption (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppPagodaOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfixingDates != nullptr) _CfixingDates->Unlock();
    }
}
Cephei::QL::Experimental::CPagodaOption::CPagodaOption (boost::shared_ptr<QuantLib::PagodaOption>& childNative, Object^ owner) : CMultiAssetOption(CPagodaOption::typeid)
{
#ifdef HANDLE
	_phPagodaOption = NULL;
#endif
	_ppPagodaOption = &childNative;
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppPagodaOption));
}
Cephei::QL::Experimental::CPagodaOption::CPagodaOption (QuantLib::PagodaOption& childNative, Object^ owner) : CMultiAssetOption(CPagodaOption::typeid)
{
#ifdef HANDLE
	_phPagodaOption = NULL;
#endif
	_ppPagodaOption = new boost::shared_ptr<QuantLib::PagodaOption> (&childNative);
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppPagodaOption));
    _PagodaOptionOwner = owner;
    _MultiAssetOptionOwner = owner;
}

Cephei::QL::Experimental::CPagodaOption::CPagodaOption (CPagodaOption^ copy) : CMultiAssetOption(CPagodaOption::typeid)
{
#ifdef HANDLE
	_phPagodaOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPagodaOption = new boost::shared_ptr<QuantLib::PagodaOption> (copy->GetShared());
        _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppPagodaOption));
    }
}
Cephei::QL::Experimental::CPagodaOption::CPagodaOption (System::Type^ t) : CMultiAssetOption(CPagodaOption::typeid)
{
#ifdef HANDLE
	_phPagodaOption = NULL;
#endif
	if (!t->IsSubclassOf(CPagodaOption::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::CPagodaOption::CPagodaOption (QuantLib::Handle<QuantLib::PagodaOption>& childNative, Object^ owner)  : CMultiAssetOption(CPagodaOption::typeid)
{
	_phPagodaOption = &childNative;
	_ppPagodaOption = &static_cast<boost::shared_ptr<QuantLib::PagodaOption>>(childNative.currentLink());
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppPagodaOption));
    _PagodaOptionOwner = owner;
}
Cephei::QL::Experimental::CPagodaOption::CPagodaOption (QuantLib::Handle<QuantLib::PagodaOption> childNative)  : CMultiAssetOption(CPagodaOption::typeid)
{
	_phPagodaOption = &childNative;
	_ppPagodaOption = &static_cast<boost::shared_ptr<QuantLib::PagodaOption>>(childNative.currentLink());
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppPagodaOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::CPagodaOption::CPagodaOption (QuantLib::PagodaOption childNative)  : CMultiAssetOption(CPagodaOption::typeid)
{
#ifdef HANDLE
	_phPagodaOption = NULL;
#endif
	_ppPagodaOption = new boost::shared_ptr<QuantLib::PagodaOption> (new QuantLib::PagodaOption (childNative));
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppPagodaOption));
}
#endif

Cephei::QL::Experimental::CPagodaOption::~CPagodaOption ()
{
    if (_ppPagodaOption != NULL)
    {
	    delete _ppPagodaOption;
        _ppPagodaOption = NULL;
    }
}
Cephei::QL::Experimental::CPagodaOption::!CPagodaOption ()
{
    if (_ppPagodaOption != NULL)
    {
	    delete _ppPagodaOption;
    }
}
QuantLib::PagodaOption& Cephei::QL::Experimental::CPagodaOption::GetReference ()
{
    if (_ppPagodaOption == NULL) throw gcnew NativeNullException ();
	return **_ppPagodaOption;
}
boost::shared_ptr<QuantLib::PagodaOption>& Cephei::QL::Experimental::CPagodaOption::GetShared ()
{
    if (_ppPagodaOption == NULL) throw gcnew NativeNullException ();
	return *_ppPagodaOption;
}
QuantLib::PagodaOption* Cephei::QL::Experimental::CPagodaOption::GetPointer ()
{
    if (_ppPagodaOption == NULL) throw gcnew NativeNullException ();
	return &**_ppPagodaOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PagodaOption>& Cephei::QL::Experimental::CPagodaOption::GetHandle ()
{
	if (_phPagodaOption == NULL)
	{
		_phPagodaOption = new Handle<QuantLib::PagodaOption> (*_ppPagodaOption);
	}
	return *_phPagodaOption;
}
#endif
bool Cephei::QL::Experimental::CPagodaOption::HasNative () 
{
	return (_ppPagodaOption != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::IPagodaOption^ Cephei::QL::Experimental::CPagodaOption_Factory::Create (Cephei::IVector<DateTime>^ fixingDates, Double roof, Double fraction, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CPagodaOption ( fixingDates,  roof,  fraction,  QL_Pricer);
}
